package com.sda.huoshui

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.sda.huoshui.base.BaseActivity
import com.sda.huoshui.bean.ShouYeBean
import com.sda.huoshui.bean.ZhuYeBottomBean
import com.sda.huoshui.fragment.ShouYeFragment
import com.sda.huoshui.util.CommonTools
import com.sda.huoshui.view.NoScrollRecyclerView
import com.sda.huoshui.view.clickDelay
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_shou_ye.*
import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class MainActivity : BaseActivity() {
    private val mBottomList: ArrayList<ZhuYeBottomBean.DataBean> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerViewBottom.adapter = MyAdapter(mBottomList)
        getAppBottom()
    }


    private fun getAppBottom() {
        val map = HashMap<String, Any>()
        mCompositeSubscription!!.add(
            retrofitService!!.getAppBottom(map).subscribeOn(Schedulers.io()).observeOn(
                AndroidSchedulers.mainThread()
            ).subscribe(
                object : Observer<ResponseBody> {
                    override fun onCompleted() {
                        cancelProgressDialog()
                    }

                    override fun onError(e: Throwable) {
                        e.printStackTrace()
                        cancelProgressDialog()
                    }

                    override fun onNext(data: ResponseBody) {
                        val str = data.string()
                        CommonTools.parsingReturnData(str,
                            object : CommonTools.OnParsingReturnListener {
                                @SuppressLint("NotifyDataSetChanged")
                                override fun onParsingSuccess() {
                                    val bean =
                                        Gson().fromJson(str, ZhuYeBottomBean::class.java).data
                                    mBottomList.clear()
                                    mBottomList.addAll(bean)
                                    if (mBottomList.size > 0) {
                                        mBottomList[0].isCheck = true
                                        supportFragmentManager.beginTransaction().add(R.id.frameLayout, ShouYeFragment()).commit()
                                        recyclerViewBottom.adapter?.notifyDataSetChanged()
                                    }
                                }
                            })
                    }
                })
        )
    }

    //列表的适配器
    inner class MyAdapter(data: List<ZhuYeBottomBean.DataBean>) :
        BaseQuickAdapter<ZhuYeBottomBean.DataBean, BaseViewHolder>(
            R.layout.item_zhuye_bottom,
            data
        ) {

        @SuppressLint("NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: ZhuYeBottomBean.DataBean) {
            val ivPic = helper.getView<ImageView>(R.id.ivPic)
            val tvName = helper.getView<TextView>(R.id.tvName)
            CommonTools.showGlideImage(
                this@MainActivity, if (item.isCheck) {
                    item.select_image
                } else {
                    item.image
                }, ivPic
            )
            helper.setText(R.id.tvName, item.name)
            tvName.isSelected = item.isCheck
            helper.getView<LinearLayout>(R.id.llAll).clickDelay {
                mBottomList.forEachIndexed { index, dataBean ->
                    dataBean.isCheck = index == helper.layoutPosition
                }
                notifyDataSetChanged()
            }
        }
    }

}